home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / _archvrs / unix / shar.lha / shar / getopt.c < prev    next >
C/C++ Source or Header  |  1987-12-03  |  1KB  |  76 lines

  1. /*
  2. **  Return options and their values from the command line.
  3. **
  4. **  This comes from the AT&T public-domain getopt published in mod.sources.
  5. */
  6. /* LINTLIBRARY */
  7. #include "shar.h"
  8. RCS("$Header: getopt.c,v 1.4 87/03/02 11:03:24 rs Exp $")
  9.  
  10. #ifdef    NEED_GETOPT
  11.  
  12.  
  13. #define TYPE    int
  14.  
  15. #define ERR(s, c)    if(opterr){\
  16.     char errbuf[2];\
  17.     errbuf[0] = c; errbuf[1] = '\n';\
  18.     (void) write(2, argv[0], (TYPE)strlen(argv[0]));\
  19.     (void) write(2, s, (TYPE)strlen(s));\
  20.     (void) write(2, errbuf, 2);}
  21.  
  22. extern int strcmp();
  23.  
  24. int    opterr = 1;
  25. int    optind = 1;
  26. int    optopt;
  27. char    *optarg;
  28.  
  29. int
  30. getopt(argc, argv, opts)
  31. int    argc;
  32. char    **argv, *opts;
  33. {
  34.     static int sp = 1;
  35.     register int c;
  36.     register char *cp;
  37.  
  38.     if(sp == 1)
  39.         if(optind >= argc ||
  40.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  41.             return(EOF);
  42.         else if(strcmp(argv[optind], "--") == NULL) {
  43.             optind++;
  44.  
  45.         }
  46.     optopt = c = argv[optind][sp];
  47.     if(c == ':' || (cp=IDX(opts, c)) == NULL) {
  48.         ERR(": illegal option -- ", c);
  49.         if(argv[optind][++sp] == '\0') {
  50.             optind++;
  51.             sp = 1;
  52.         }
  53.         return('?');
  54.     }
  55.     if(*++cp == ':') {
  56.         if(argv[optind][sp+1] != '\0')
  57.             optarg = &argv[optind++][sp+1];
  58.         else if(++optind >= argc) {
  59.             ERR(": option requires an argument -- ", c);
  60.             sp = 1;
  61.             return('?');
  62.         } else
  63.             optarg = argv[optind++];
  64.         sp = 1;
  65.     } else {
  66.         if(argv[optind][++sp] == '\0') {
  67.             sp = 1;
  68.             optind++;
  69.         }
  70.         optarg = NULL;
  71.     }
  72.     return(c);
  73. }
  74.  
  75. #endif    /* NEED_GETOPT */
  76.